<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>添加关键词配置</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 20px;
            background-color: #f5f5f5;
        }
        .container {
            max-width: 600px;
            margin: 0 auto;
            background-color: white;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }
        h1 {
            color: #333;
            text-align: center;
        }
        .form-group {
            margin-bottom: 15px;
        }
        .form-group label {
            display: block;
            margin-bottom: 5px;
            font-weight: bold;
        }
        .form-group input, .form-group select {
            width: 100%;
            padding: 8px;
            border: 1px solid #ddd;
            border-radius: 4px;
            box-sizing: border-box;
        }
        .form-group input[type="checkbox"] {
            width: auto;
        }
        .btn {
            display: inline-block;
            padding: 10px 20px;
            margin: 5px;
            background-color: #007bff;
            color: white;
            text-decoration: none;
            border-radius: 4px;
            border: none;
            cursor: pointer;
        }
        .btn:hover {
            background-color: #0056b3;
        }
        .btn-secondary {
            background-color: #6c757d;
        }
        .btn-secondary:hover {
            background-color: #5a6268;
        }
        .message {
            padding: 10px;
            margin: 10px 0;
            border-radius: 4px;
        }
        .message.success {
            background-color: #d4edda;
            color: #155724;
            border: 1px solid #c3e6cb;
        }
        .message.error {
            background-color: #f8d7da;
            color: #721c24;
            border: 1px solid #f5c6cb;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>添加关键词配置</h1>
        
        <form id="configForm">
            <div class="form-group">
                <label for="keyword">关键词:</label>
                <input type="text" id="keyword" name="keyword" required>
            </div>
            
            <div class="form-group">
                <label for="count">收集数量:</label>
                <input type="number" id="count" name="count" value="5" min="1" max="100" required>
            </div>
            
            <div class="form-group">
                <label for="platform">平台:</label>
                <select id="platform" name="platform">
                    <option value="douyin">抖音</option>
                    <option value="tiktok">TikTok</option>
                </select>
            </div>
            
            <div class="form-group">
                <label for="source_lang">原语言:</label>
                <select id="source_lang" name="source_lang">
                    <option value="en">英语</option>
                    <option value="zh" selected>中文</option>
                    <option value="th">泰语</option>
                    <option value="vi">越南语</option>
                </select>
            </div>
            
            <div class="form-group">
                <label for="target_lang">目标语言:</label>
                <select id="target_lang" name="target_lang">
                    <option value="vi" selected>越南语</option>
                    <option value="th">泰语</option>
                    <option value="pt-br">葡萄牙语-巴西</option>
                </select>
            </div>
            
            <div class="form-group">
                <label for="enabled">
                    <input type="checkbox" id="enabled" name="enabled" checked>
                    启用配置
                </label>
            </div>
            
            <div class="form-group">
                <button type="submit" class="btn">添加配置</button>
                <a href="/" class="btn btn-secondary">返回列表</a>
            </div>
        </form>
        
        <div id="message" class="message" style="display: none;"></div>
    </div>

    <script>
        document.getElementById('configForm').addEventListener('submit', function(e) {
            e.preventDefault();
            
            const formData = new FormData(this);
            const data = {};
            formData.forEach((value, key) => {
                if (key === 'enabled') {
                    data[key] = !!value;
                } else {
                    data[key] = value;
                }
            });
            
            fetch('/add', {
                method: 'POST',
                body: new URLSearchParams(data),
                headers: {
                    'Content-Type': 'application/x-www-form-urlencoded',
                }
            })
            .then(response => response.json())
            .then(data => {
                const messageDiv = document.getElementById('message');
                messageDiv.style.display = 'block';
                if (data.success) {
                    messageDiv.className = 'message success';
                    messageDiv.textContent = data.message;
                    // 清空表单
                    document.getElementById('configForm').reset();
                } else {
                    messageDiv.className = 'message error';
                    messageDiv.textContent = data.message;
                }
                
                // 3秒后隐藏消息
                setTimeout(() => {
                    messageDiv.style.display = 'none';
                }, 3000);
            })
            .catch(error => {
                const messageDiv = document.getElementById('message');
                messageDiv.style.display = 'block';
                messageDiv.className = 'message error';
                messageDiv.textContent = '添加失败: ' + error.message;
                
                setTimeout(() => {
                    messageDiv.style.display = 'none';
                }, 3000);
            });
        });
    </script>
</body>
</html>